package com.haoan.index.utils.wx;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.haoan.common.exception.ServiceException;
import com.haoan.common.utils.CompressImgUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: Kai
 * @Date: 2024/9/10
 * @Description: com.haoan.utils.wx
 */
@Slf4j
public class WxUtil {
    private static String access_token = getAccessToken();
    private static Date expiresTime;


    /**
     * 获取openid
     */
    public static String getOpenId(String code) {
        String getOpenidResp = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session?appid=" + WxpayConstant.APP_ID + "&secret=" + WxpayConstant.APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code");
        JSONObject respJson = JSONUtil.parseObj(getOpenidResp);
        String openid = respJson.getStr("openid");
        String session_key = respJson.getStr("session_key");
        return openid;
    }

    /**
     * 获取accessToken
     */
    private static String getAccessToken() {
        Date now = new Date();
        if (expiresTime == null || expiresTime.before(now)) {
            String getAccessResp = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?appid=" + WxpayConstant.APP_ID + "&secret=" + WxpayConstant.APP_SECRET + "&grant_type=client_credential");
            JSONObject getAccessRespJson = JSONUtil.parseObj(getAccessResp);
            access_token = getAccessRespJson.getStr("access_token");
            Long expires_in = getAccessRespJson.getLong("expires_in");
            expiresTime = DateUtil.offset(now, DateField.SECOND, expires_in.intValue());
        }
        return access_token;
    }

    public static String getPhone(String code, String openid) {
        if (StrUtil.isBlank(code)) {
            return null;
        }
        String phoneNumber = null;
        String phoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber" + "?access_token=" + getAccessToken();
        JSONObject phoneRequestBody = new JSONObject();
        phoneRequestBody.set("code", code);
        phoneRequestBody.set("openid", openid);
        String getPhoneResp = HttpRequest.post(phoneUrl).header("Content-Type", "application/json").body(phoneRequestBody.toString())
                .execute().body();
        JSONObject getPhoneRespJson = JSONUtil.parseObj(getPhoneResp);
        String phoneErrCode = getPhoneRespJson.getStr("errcode");
        if ("0".equals(phoneErrCode)) {
            phoneNumber = getPhoneRespJson.getJSONObject("phone_info").getStr("phoneNumber");
        }
        return phoneNumber;
    }


    public static void main(String[] args) {
    }

}
